package com.have.blog.admin.redis.impl;

import com.have.blog.admin.redis.AdminSysDictDataCacheService;
import com.have.blog.base.constant.BaseRedisConstant;
import com.have.blog.config.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class AdminSysDictDataCacheServiceImpl implements AdminSysDictDataCacheService {
    @Value("${redis.admin.database}")
    private String REDIS_KEY_DATABASE;
    @Autowired
    private RedisService redisService;

    @Override
    public Map<String, Object> getDictData(String dictType) {
        String key = REDIS_KEY_DATABASE + BaseRedisConstant.SEGMENTATION +
                BaseRedisConstant.REDIS_DICT_TYPE + BaseRedisConstant.SEGMENTATION +
                dictType;
        return (Map<String, Object>) redisService.get(key);
    }

    @Override
    public void saveDictData(Map<String, Object> data, String dictType, long expiration, TimeUnit timeUnit) {
        String key = REDIS_KEY_DATABASE + BaseRedisConstant.SEGMENTATION +
                BaseRedisConstant.REDIS_DICT_TYPE + BaseRedisConstant.SEGMENTATION +
                dictType;
        redisService.set(key, data, expiration, timeUnit);
    }

    @Override
    public void delDictData(String dictType) {
        String key = REDIS_KEY_DATABASE + BaseRedisConstant.SEGMENTATION +
                BaseRedisConstant.REDIS_DICT_TYPE + BaseRedisConstant.SEGMENTATION +
                dictType;
        redisService.del(key);
    }

    @Override
    public Map<String, Object> getDataListByDictType(String dictType) {
        String key = REDIS_KEY_DATABASE + BaseRedisConstant.SEGMENTATION +
                BaseRedisConstant.REDIS_DICT_TYPE + BaseRedisConstant.SEGMENTATION + dictType;
        return (Map<String, Object>) redisService.get(key);
    }

    @Override
    public void saveDataListByDictType(Map<String, Object> data, String dictType, long expiration, TimeUnit timeUnit) {
        String key = REDIS_KEY_DATABASE + BaseRedisConstant.SEGMENTATION +
                BaseRedisConstant.REDIS_DICT_TYPE + BaseRedisConstant.SEGMENTATION + dictType;
        redisService.set(key, data, expiration, timeUnit);
    }

    @Override
    public void delDataListByDictType(String dictType) {
        String key = REDIS_KEY_DATABASE + BaseRedisConstant.SEGMENTATION +
                BaseRedisConstant.REDIS_DICT_TYPE + BaseRedisConstant.SEGMENTATION + dictType;
        redisService.del(key);
    }
}
